#include "noise.h"
#include <qmath.h>

Noise::Noise(int seed)
{
    offset = (double) seed / 1000.0;
}

double Noise::getSineAt(double x, double wavelength)
{
    if(wavelength > 0.0)
        return sin(x / wavelength * 2.0 * M_PI + offset);
}

double Noise::getMultiSineAt(double x, double wavelength, int harmonics, double scale, double dampen)
{
    double sum = 0.0;
    double max = 0.0;

    if(wavelength > 0.0)
    {
        for(int i = 0; i < harmonics; i++)
        {
            sum += sin(x / wavelength * 2.0 / pow(scale, i) * M_PI + offset) * pow(dampen, i);
            max += pow(dampen, i);
        }
    }

    if(max > 0.0)
        return sum / max;
    else
        return 0.0;
}
